$(function() {

	getFolder();
	$('.tab-item').click(function(){
		$('.tab-item').removeClass('tab-item-check');
		$(this).addClass('tab-item-check');
		getFolder();
	});
}); 

var paramFunc = function() { 
	var data = getFormJson();
	return data; 
};


function getFolder(){
	doBack();
	var load_alert=loadAlert('加载中...');
	$('.folder-box').html('');
	$.ajax({
		type: "post",
		url: "Admin/SupervisionOther/getFolder/"+$('.tab-item-check').attr('data-type'),
		async: true,
		dataType:'json',
		success: function(data) {
			closeAlert(load_alert);
			if(data.status) {
				var t=JSON.parse(data.v);
				$.each(t,function(k,v){
					$('.folder-demo .img').attr('src',v.img).attr('data-id',v.id);
					$('.folder-demo .delete').attr('data-id',v.id);
					$('.folder-demo .reset-name').attr('data-id',v.id);
					$('.folder-demo .title').html(sub_str(v.name,6)).attr('title',v.name);
					$('.folder-box').append($('.folder-demo').html());
				});
			} else {
				window.parent.topMessagePrompt('error',data.msg);
			}
		}
	});
}

function addFolder(){
	showDialogModalConfirm('confirm','请输入文件夹名称','<input type="text" class="new_name" value="新建文件夹" style="background-color:#eee; text-indent:5px;"/><script>$(".new_name").focus()</script>' ,'确认','取消',function(){ 
		var load_alert=loadAlert('操作中...');
		$.ajax({
			type: "post",
			url: "Admin/SupervisionOther/addFolder/"+$('.tab-item-check').attr('data-type'),
			async: true,
			data:{name:$('.new_name').val()},
			dataType:'json',
			success: function(data) {
				closeAlert(load_alert);
				if(data.status) {
					var t=JSON.parse(data.v);
					$('.folder-demo .img').attr('src',t.img).attr('data-id',t.id);
					$('.folder-demo .delete').attr('data-id',t.id);
					$('.folder-demo .reset-name').attr('data-id',t.id);
					$('.folder-demo .title').html(sub_str(t.name,6)).attr('title',t.name);
					$('.folder-box').append($('.folder-demo').html());
				} else {
					window.parent.topMessagePrompt('error',data.msg);
				}
			}
		});
	});
}


function delFolder(obj){
	var msg = "删除文件夹，将会同步删除该文件夹下所有文件，确认继续？";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			var load_alert=loadAlert('删除中...');
			$.ajax({
				type: "post",
				url: "Admin/SupervisionOther/delFolder/"+$(obj).attr('data-id'),
				async: true,
				dataType:'json',
				success: function(data) {
					closeAlert(load_alert);
					if(data.status) {
						$(obj).parents('.folder-content').remove();
					} else {
						window.parent.topMessagePrompt('error',data.msg);
					}
				}
			});
		}
	});
}

function resetName(obj){
	showDialogModalConfirm('confirm','请输入文件夹名称','<input type="text" class="new_name" value="'+$(obj).parents('.folder-content').find('.title').attr('title')+'" style="background-color:#eee; text-indent:5px;"/><script>$(".new_name").focus()</script>' ,'确认','取消',function(){ 
		var load_alert=loadAlert('修改中...');
		$.ajax({
			type: "post",
			url: "Admin/SupervisionOther/resetName/"+$(obj).attr('data-id'),
			async: true,
			dataType:'json',
			data:{name:$('.new_name').val()},
			success: function(data) {
				closeAlert(load_alert);
				if(data.status) {
					var t=JSON.parse(data.v);
					$(obj).parents('.folder-content').find('.title').html(sub_str(t.name,6)).attr('title',t.name);
				} else {
					window.parent.topMessagePrompt('error',data.msg);
				}
			}
		});
	});
}

function showDocument(obj){
	$('.document-box').show();
	$('.document-operation').show();
	$('.folder-box').hide();
	$('.folder-operation').hide();
	$('#now-folder-id').val($(obj).attr('data-id'));
	$('.document-box').html($('.document-add-demo').html());
	var load_alert=loadAlert('获取中...');
	$.ajax({
		type: "post",
		url: "Admin/SupervisionOther/getDocument/"+$(obj).attr('data-id'),
		async: true,
		dataType:'json',
		data:{name:$('.new_name').val()},
		success: function(data) {
			closeAlert(load_alert);
			if(data.status) {
				var t=JSON.parse(data.v);
				$.each(t,function(k,v){
					$('.document-demo .img').attr('data-id',v.id);
					$('.document-demo .delete').attr('data-id',v.id);
					$('.document-demo .reset-name').attr('data-id',v.id);
					$('.document-demo .title').html(sub_str(v.name,6)).attr('title',v.name);
					$('.document-box').append($('.document-demo').html());
				});
			} else {
				window.parent.topMessagePrompt('error',data.msg);
			}
		}
	});
}

function doBack(){
	$('.document-box').hide();
	$('.document-operation').hide();
	$('.folder-box').show();
	$('.folder-operation').show();
	$('#now-folder-id').val('');
	$('.document-box').html($('.document-add-demo').html());
}



function resetDocumentName(obj){
	showDialogModalConfirm('confirm','请输入文件夹名称','<input type="text" class="new_name" value="'+$(obj).parents('.document-content').find('.title').attr('title')+'" style="background-color:#eee; text-indent:5px;"/><script>$(".new_name").focus()</script>' ,'确认','取消',function(){ 
		var load_alert=loadAlert('修改中...');
		$.ajax({
			type: "post",
			url: "Admin/SupervisionOther/resetDocumentName/"+$(obj).attr('data-id'),
			async: true,
			dataType:'json',
			data:{
				name:$('.new_name').val(),
				type:$('.tab-item-check').attr('data-type')
			},
			success: function(data) {
				closeAlert(load_alert);
				if(data.status) {
					var t=JSON.parse(data.v);
					$(obj).parents('.document-content').find('.title').html(sub_str(t.name,6)).attr('title',t.name);
				} else {
					window.parent.topMessagePrompt('error',data.msg);
				}
			}
		});
	});
}


function delDocument(obj){
	var msg = "删除的文件将无法恢复，确认继续？";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			var load_alert=loadAlert('删除中...');
			$.ajax({
				type: "post",
				url: "Admin/SupervisionOther/delDocument/"+$(obj).attr('data-id'),
				async: true,
				data:{
					type:$('.tab-item-check').attr('data-type')
				},
				dataType:'json',
				success: function(data) {
					closeAlert(load_alert);
					if(data.status) {
						$(obj).parents('.document-content').remove();
						//修改当前文件夹的
						$('.folder-content .img').each(function(){
							if($(this).attr('data-id')==$('#now-folder-id').val()){
								$(this).attr('src',data.v);
							}
						});
					} else {
						window.parent.topMessagePrompt('error',data.msg);
					}
				}
			});
		}
	});
	
}


function addDocument(){
	showPageDialogModal(getRootPath()+'/Admin/SupervisionOther/tableEdit'+$('.tab-item-check').attr('data-type')+'/0/'+$('#now-folder-id').val(),700,500);
}

function editDocument(obj){
	showPageDialogModal(getRootPath()+'/Admin/SupervisionOther/tableEdit'+$('.tab-item-check').attr('data-type')+'/'+$(obj).attr('data-id')+'/'+$('#now-folder-id').val(),700,500);
}